package com.pipi.mq.rocketmq.base.producer;

import com.alibaba.fastjson.JSON;
import com.pipi.mq.rocketmq.base.config.PropertiesConfig;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.utils.ThreadUtils;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.*;

/**
 * @Package com.pipi.mq.rocketmq.base
 * @author: 皮皮（pipi@maihaoche.com）
 * @date: 2020/4/18 1:25 PM
 */
@Component
public class SyncProducer extends AbstractMQProducer {


    public SyncProducer() {
        setTopic(PropertiesConfig.topicSync);
        setTag(PropertiesConfig.tagSync);
    }

    public void syncSendThread(CountDownLatch countDownLatch) {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                1,
                100L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(10));
        executorService.submit(() -> {
            syncSend();
            countDownLatch.countDown();
        });
        executorService.shutdown();
    }

    public void syncSend() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            // 发送消息到broker
//            String s = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "  syncSendMessage-" + i;
            String s = "yyyy-MM-dd HH:mm:ss" +  "  syncSendMessage-" + 0;
            synSend("",s,s);
        }
        System.out.println(System.currentTimeMillis() - startTime);

    }
}
